package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import com.cloudbroker.bcs.platform.dav.bizapi.service.base.vo.HSBCPSVO;
import org.springframework.stereotype.Service;

import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.MarketTrend;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.upmarket.UpDownMarketBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.upmarket.vo.UpDownMarketForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.upmarket.vo.UpDownMarketNum;
import com.cloudbroker.bcs.platform.dav.common.constant.CacheKeys;
import com.cloudbroker.bcs.platform.dav.common.util.CacheHelper;
import com.cloudbroker.bcs.platform.dav.middle.context.HSBCPSBizDataContext;

@Service
public class UpDownMarketBizServiceImpl implements UpDownMarketBizService {

    @Override
    public UpDownMarketNum funcUpDownMarket(UpDownMarketForm param) {
        Long customer_id = HSBCPSBizDataContext.getCustomer().getCustomer_id();
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        String market_trend = param.getMarket_trend();
        String upMarket = CacheKeys.UP_MARKET + broker_id;
        String downMarket = CacheKeys.DOWN_MARKET + broker_id;
        String content = "" + customer_id;
        UpDownMarketNum upDownMarketNum = new UpDownMarketNum();
        CacheHelper cacheHelper = CacheHelper.getInstance();
        if (!cacheHelper.sismember(downMarket, content) && !cacheHelper.sismember(upMarket, content)) {
            if (!StringUtil.isEmpty(market_trend) && String.valueOf(MarketTrend.UP.getVal()).equals(market_trend)) {
                cacheHelper.sdd(upMarket,content);
                if (!cacheHelper.sismember(CacheKeys.UP_MARKET,upMarket)) {
                    cacheHelper.sdd(CacheKeys.UP_MARKET, upMarket);
                }
                upDownMarketNum.setUp_status(1);
            } else if (!StringUtil.isEmpty(market_trend) && String.valueOf(MarketTrend.DOWN.getVal()).equals(market_trend)) {
                cacheHelper.sdd(downMarket,content);
                if (!cacheHelper.sismember(CacheKeys.DOWN_MARKET,downMarket)) {
                    cacheHelper.sdd(CacheKeys.DOWN_MARKET, downMarket);
                }
                upDownMarketNum.setUp_status(2);
            }
        } else if (cacheHelper.sismember(downMarket, content)){
            upDownMarketNum.setUp_status(2);
        } else {
            upDownMarketNum.setUp_status(1);
        }

        if (CacheHelper.ttl(upMarket) == -1){
            CacheHelper.expire(upMarket, 24*60*60);
        }
        if (CacheHelper.ttl(downMarket) == -1){
            CacheHelper.expire(downMarket, 24*60*60);
        }

        upDownMarketNum.setUpNum(cacheHelper.scard(upMarket));
        upDownMarketNum.setDownNum(cacheHelper.scard(downMarket));
        return upDownMarketNum;
    }

    @Override
    public UpDownMarketNum funcQryMarket(HSBCPSVO param) {
        Long customer_id = HSBCPSBizDataContext.getCustomer().getCustomer_id();
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        String upMarket = CacheKeys.UP_MARKET + broker_id;
        String downMarket = CacheKeys.DOWN_MARKET + broker_id;
        String content = "" + customer_id;
        UpDownMarketNum upDownMarketNum = new UpDownMarketNum();
        CacheHelper cacheHelper = CacheHelper.getInstance();
        if (cacheHelper.sismember(downMarket, content)){
            upDownMarketNum.setUp_status(2);
        } else if (cacheHelper.sismember(upMarket, content)){
            upDownMarketNum.setUp_status(1);
        }

        if (CacheHelper.ttl(upMarket) == -1){
            CacheHelper.expire(upMarket, 24*60*60);
        }
        if (CacheHelper.ttl(downMarket) == -1){
            CacheHelper.expire(downMarket, 24*60*60);
        }

        upDownMarketNum.setUpNum(cacheHelper.scard(upMarket));
        upDownMarketNum.setDownNum(cacheHelper.scard(downMarket));
        return upDownMarketNum;
    }
}
